<template>
  <el-dialog
    v-el-drag-dialog
    :title="type === 'add' ? '新增角色' : '编辑角色'"
    :visible.sync="visible"
    width="600px"
    @closed="reset"
  >
    <el-form ref="dataForm" :rules="rules" :model="temp" label-position="right" label-width="90px">
      <el-form-item label="角色名称:" prop="roleName">
        <el-input v-model="temp.roleName" maxlength="30" />
      </el-form-item>
      <el-form-item label="角色类型:" prop="roleType">
        <el-select v-model="temp.roleType" style="width: 100%;" clearable :disabled="type==='edit'">
          <el-option
            v-for="item in roleTypeMap"
            :key="item.value"
            :label="item.desc"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="remark" label="备注:">
        <el-input v-model="temp.remark" type="textarea" :rows="3" maxlength="80" show-word-limit />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        取消
      </el-button>
      <el-button :loading="$loadingPlugin.add || $loadingPlugin.edit" type="primary" @click="success">
        确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { roleAdd } from '@/api'

  export default {
    name: 'add-role',
    props: {
      roleTypeMap: {
        type: Array,
        required: true
      }
    },
    data() {
      return {
        visible: false,
        type: 'add',
        temp: {
          roleId: 0,
          roleName: '',
          roleType: '',
          remark: ''
        },
        rules: {
          roleName: [{ required: true, message: '请输入角色名称', trigger: 'change' }],
          roleType: [{ required: true, message: '请选择角色类型', trigger: 'change' }]
        }
      }
    },
    methods: {
      open(temp) {
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
          if (temp) {
            this.temp = Object.assign(this.temp, temp)
            this.type = 'edit'
            this.temp.roleId = temp.roleId
          }
        })
      },
      success() {
        this.$refs.dataForm.validate(valid => {
          if (!valid) return
          const params = {
            roleId: this.temp.roleId,
            roleName: this.temp.roleName,
            roleType: this.temp.roleType,
            remark: this.temp.remark
          }
          if (this.type === 'add') {
            this.add(params)
          } else {
            this.edit(params)
          }
        })
      },
      async add(params) {
        await roleAdd(params)
        this.visible = false
        this.resolve()
        this.$notify({
          title: '提示',
          message: '新增成功',
          type: 'success',
          duration: 2000
        })
      },
      async edit(params) {
        await roleAdd(params)
        this.visible = false
        this.resolve(this.temp)
        this.$notify({
          title: '提示',
          message: '更新成功',
          type: 'success',
          duration: 2000
        })
      },
      reset() {
        this.type = 'add'
        this.temp = {
          roleId: 0,
          roleName: '',
          roleType: '',
          remark: ''
        }
        this.$nextTick(() => {
          this.$refs.dataForm.clearValidate()
        })
      }
    }
  }
</script>

<style scoped>

</style>
